package view.render
{
	import enums.main.UISize;
	
	import feathers.display.Scale3Image;
	
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.display.Sprite;
	import starling.text.TextField;
	import starling.utils.Color;
	
	import util.FG2FUGenerator;
	
	
	/**
	 * item基类
	 * @author £iang
	 * 
	 */	
	public class BaseItem extends Sprite
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 背景 */
		public var bgRect:Scale3Image;
		/** 第一列标签 */
		public var firstLabel:TextField;
		/** 第一列标签占总宽的百分比 */
		public var _firstPercent:Number;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** item高度 */
		protected var _itemH:Number;
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		protected function initialize():void
		{
			this.alpha = 0;
			
			bgRect = FG2FUGenerator.drawRoundRectImage2(
				_itemH, 
				Color.WHITE, UISize.DPI_SCALE, 12, "default"
			);
			bgRect.width = UISize.MAIN_W;
			bgRect.height = _itemH * UISize.DPI_SCALE;
			this.addChild( bgRect );
			
			firstLabel = new TextField(
				UISize.MAIN_W * _firstPercent - 
					UISize.TITLE_PADDING_LEFT * UISize.DPI_SCALE, 
				_itemH * UISize.DPI_SCALE,
				"", "微软雅黑,宋体", 24 * UISize.DPI_SCALE, Color.WHITE
			);
			firstLabel.vAlign = "center";
			firstLabel.hAlign = "left";
			this.addChild( firstLabel );
			
			initializeOther();
			draw();
			bgRect.width = 0;
		}
		
		/**
		 * 初始化其他组件 
		 * 
		 */		
		protected function initializeOther():void
		{
			
		}
		
		/**
		 * 调整组件 
		 * 
		 */		
		protected function draw():void
		{
			
		}
		
		/**
		 * 播放显示动画 
		 * @param index item序号
		 * 
		 */		
		public function playShowAnimate( index:int ):void
		{
			var tween:Tween = new Tween( this, 0.4, "easeIn" );
			
			tween.animate( "alpha", 1);
			tween.delay = index * 0.1;
			Starling.juggler.add( tween );
			
			tween = new Tween( bgRect, 0.4, "easeIn" );
			
			tween.animate( "width", UISize.MAIN_W);
			tween.delay = index * 0.1;
			Starling.juggler.add( tween );
		}
		
		/**
		 * 播放隐藏动画 
		 * @param index item序号
		 * 
		 */		
		public function playHideAnimate( index:int, offset:int ):void
		{
			var tween:Tween = new Tween( this, 0.4, "easeIn" );
			
			tween.animate( "alpha", 0);
			tween.delay = ( index - offset ) * 0.1;
			Starling.juggler.add( tween );
			
			tween = new Tween( bgRect, 0.4, "easeIn" );
			
			tween.animate( "width", 0);
			tween.delay = ( index - offset ) * 0.1;
			Starling.juggler.add( tween );
		}
		
		/**
		 * 跳过显示动画 
		 * 
		 */		
		public function skipShowAnimate():void
		{
			this.alpha = 1;
			bgRect.width = UISize.MAIN_W;
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */		
		override public function dispose():void
		{
			Starling.juggler.removeTweens( bgRect );
			Starling.juggler.removeTweens( this );
			
			bgRect.dispose();
			bgRect.textures.texture.dispose();		
			bgRect = null;
			firstLabel.dispose();
			firstLabel = null;
			
			disposeOther();
			super.dispose();
		}
		
		/**
		 * 析构附加组件 
		 * 
		 */		
		protected function disposeOther():void
		{
			
		}
			
		public function disposeLabels( arr:Array ):void
		{
			var len:int = arr.length;
			
			for ( var i:int = 0; i < len; i ++ )
			{
				this[ arr[ i ] + "Label" ].dispose();
				this[ arr[ i ] + "Label" ] = null;
			}
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function BaseItem()
		{
			super();
		}
	}
}